<template>
	<el-dialog v-model="visible" title="导入表" :close-on-click-modal="false" width="70%">
		<el-card class="layout-query-card" shadow="hover">
			<el-form ref="queryRef" :model="state.queryForm" :inline="true" :rules="dataRules" @keyup.enter="getDataList()">
				<el-form-item label="项目" prop="projectId">
					<el-select v-model="state.queryForm.projectId" style="width: 180px" placeholder="请选择项目" clearable filterable>
						<el-option v-for="projectItem in projectList" :key="projectItem.id" :label="projectItem.projectName" :value="projectItem.id">
							<span style="font-weight: bold">{{ projectItem.projectName }}</span>
							<span v-if="projectItem.projectDesc && projectItem.projectDesc.trim()" style="color: #999; font-size: 12px">
								（{{ projectItem.projectDesc }}）
							</span>
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="表名" prop="tableName" :label-width="80">
					<el-input v-model="state.queryForm.tableName" placeholder="请输入表名" clearable style="width: 100%"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" :loading="state.dataListLoading" :icon="Search" @click="getDataList()">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button :icon="Refresh" @click="resetQueryHandle()">重置</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card shadow="hover">
			<el-table
				v-loading="state.dataListLoading"
				:data="state.dataList"
				border
				style="width: 100%"
				:max-height="450"
				class="layout-table"
				header-cell-class-name="layout-table-header"
				@selection-change="selectionChangeHandle"
			>
				<el-table-column type="selection" header-align="center" align="center" width="60" :selectable="(row: any) => !row.exist"></el-table-column>
				<el-table-column type="index" :index="tableIndex" label="序号" header-align="center" align="center" width="60"></el-table-column>
				<el-table-column prop="databaseName" label="数据库名称" header-align="center" align="center"></el-table-column>
				<el-table-column prop="tableName" label="表名" header-align="center" align="center"></el-table-column>
				<el-table-column prop="tableComment" label="注释" header-align="center" align="center"></el-table-column>
			</el-table>
		</el-card>
		<template #footer>
			<el-button type="primary" :icon="Check" :disabled="state.dataListSelections!.length === 0" :loading="submitLoading" @click="submitData()">
				确定
			</el-button>
			<el-button :icon="Close" @click="visible = false">取消</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, shallowReactive } from 'vue'
import { ElMessage } from 'element-plus/es'
import { Check, Close, Refresh, Search } from '@element-plus/icons-vue'
import useTableAction, { IHooksOptions } from '@/hooks/use-table-action'
import { SubmitOptions, useSubmitHandler } from '@/hooks/use-submit-handler'
import { genProjectApi, genTableApi } from '@/api'
import { GenProjectEntity, GenTableEntity, GenTableQuery } from '@/types'

defineOptions({
	name: 'GenTableImport'
})

const emit = defineEmits(['refreshDataList'])

const state = reactive({
	primaryKey: 'tableName',
	createdIsNeed: false,
	resetQueryIsNeed: false,
	isPage: false,
	dataListApi: genProjectApi.tableList,
	queryForm: {
		projectId: '',
		tableName: ''
	}
} as IHooksOptions<GenTableQuery, GenTableEntity>)

const visible = ref(false)
const projectList = ref([] as GenProjectEntity[])

const dataRules = reactive({
	projectId: [{ required: true, message: '项目不能为空', trigger: 'blur' }]
})

// 初始化操作
const init = () => {
	visible.value = true
	state.dataList = []
	submitLoading.value = false

	// 重置表单数据
	resetQueryHandle()

	genProjectApi.entityList().then(data => {
		projectList.value = data
	})
}

// 表单提交
const submitData = () => {
	if (state.dataListSelections!.length === 0) {
		ElMessage.warning('请选择表')
		return
	}
	const dataForm = {
		tableNameList: state.dataListSelections,
		projectId: state.queryForm.projectId
	}
	submitHandle(dataForm)
}

const { getDataList, selectionChangeHandle, queryRef, resetQueryHandle, tableIndex } = useTableAction(state)

const submitState = shallowReactive({
	visible,
	submitApi: genTableApi.importData,
	emit
} as SubmitOptions)

const { submitLoading, submitHandle } = useSubmitHandler(submitState)

defineExpose({
	init
})
</script>
